using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;
using Marlimer.Common.EntidadesGrilla;

namespace Marlimer.Data.Comercial
{
    public class com_DocumentoCab
    {
        public resulOMsgError guardarDocumento(InfoDocumentoCab doc)
        {
            resulOMsgError resul = new resulOMsgError(false, "", null);
            SqlCommand sqlcmd = new SqlCommand();
            Int64 cnewcod_doc = new Int64(); 

            //creamos la transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("savedoc");

            try
            {
                sqlcmd.CommandText = "spiaDocumentoCab";
                sqlcmd.CommandType= CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();
                sqlcmd.Transaction = transac;

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@ccod_cia", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cnum_docvta", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@dfec_emi", SqlDbType.DateTime);
                sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
                sqlcmd.Parameters.Add("@ccod_cli", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_dirent", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar,15);
                sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_vend", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_trans", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_mone", SqlDbType.Int);
                sqlcmd.Parameters.Add("@nval_mone", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ntot_bruto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_noafec", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_dcto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_dctonoafec", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npor_igv", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@autorizar", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cobservacion", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cmodo", SqlDbType.Int);

                sqlcmd.Parameters["@ccod_doc"].Value =  doc.ccod_doc;
                sqlcmd.Parameters["@ccod_cia"].Value = doc.ccod_cia;
                sqlcmd.Parameters["@ctip_docvta"].Value = doc.ctip_docvta;
                sqlcmd.Parameters["@cnum_serie"].Value =  doc.cnum_serie;
                sqlcmd.Parameters["@cnum_docvta"].Value = doc.cnum_docvta;
                sqlcmd.Parameters["@dfec_emi"].Value = doc.dfec_emi;
                sqlcmd.Parameters["@dfec_ent"].Value = doc.dfec_ent;
                sqlcmd.Parameters["@ccod_cli"].Value = doc.ccod_cli;
                sqlcmd.Parameters["@ccod_dirent"].Value = doc.ccod_dirent;
                sqlcmd.Parameters["@ccod_modulo"].Value = doc.ccod_modulo;
                sqlcmd.Parameters["@ccod_ruta"].Value = doc.ccod_ruta;
                sqlcmd.Parameters["@ccod_vend"].Value = doc.ccod_vend;
                sqlcmd.Parameters["@ccod_trans"].Value = doc.ccod_trans;
                sqlcmd.Parameters["@ccod_mone"].Value = doc.ccod_mone;
                sqlcmd.Parameters["@nval_mone"].Value = doc.nval_mone; 
                sqlcmd.Parameters["@ccod_condvta"].Value = doc.ccod_condvta;
                sqlcmd.Parameters["@ntot_bruto"].Value = doc.ntot_bruto;
                sqlcmd.Parameters["@ntot_noafec"].Value = doc.ntot_noafec;
                sqlcmd.Parameters["@ntot_dcto"].Value = doc.ntot_dcto;
                sqlcmd.Parameters["@ntot_dctonoafec"].Value = doc.ntot_dctonoafec;
                sqlcmd.Parameters["@npor_igv"].Value = doc.npor_igv; 
                sqlcmd.Parameters["@autorizar"].Value = doc.autorizar;
                sqlcmd.Parameters["@cobservacion"].Value = doc.cobservacion;
                sqlcmd.Parameters["@cpc_crea"].Value = doc.cpc_crea;
                sqlcmd.Parameters["@cuser_crea"].Value = doc.cuser_crea;
                sqlcmd.Parameters["@cmodo"].Value = doc.cmodo;

                long ccod_doc = 0;
                if (doc.cmodo == 1)
                {//insertar
                    ccod_doc = Convert.ToInt64(sqlcmd.ExecuteScalar());
                }
                else
                {//actualizar
                    ccod_doc = doc.ccod_doc;
                    sqlcmd.ExecuteNonQuery();
                }
                sqlcmd.CommandText = "spiDocumentoDet";
                sqlcmd.Transaction = transac;
                sqlcmd.Parameters.Clear();

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@ccod_prod", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ncant", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_unimed", SqlDbType.Int);
                sqlcmd.Parameters.Add("@npre_vta", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npor_dcto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@flg_noafecigv", SqlDbType.Int);
                sqlcmd.Parameters.Add("@npor_igv", SqlDbType.Decimal);

                foreach (InfoDocumentoDet docdet in doc.documentoDet)
                {
                    sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
                    sqlcmd.Parameters["@ccod_prod"].Value = docdet.ccod_prod;
                    sqlcmd.Parameters["@ncant"].Value = docdet.ncant;
                    sqlcmd.Parameters["@ccod_unimed"].Value = docdet.ccod_unimed;
                    sqlcmd.Parameters["@npre_vta"].Value = docdet.npre_vta;
                    sqlcmd.Parameters["@npor_dcto"].Value = docdet.npor_dcto;
                    sqlcmd.Parameters["@flg_noafecigv"].Value = docdet.flg_noafecigv;
                    sqlcmd.Parameters["@npor_igv"].Value = docdet.npor_igvdet();
                    sqlcmd.ExecuteNonQuery();
                }
                //terminamos la transaccion
                transac.Commit();

                resul.message = "Se guardo el documento satisfactoriamente.";
                cnewcod_doc = (Int64)ccod_doc;
                resul.clase = cnewcod_doc;
                resul.resul = true;
            }
            catch (Exception ex)
            {
                transac.Rollback(); 
                resul.message = ex.Message; 
            }

            return resul;
        }

        public DataTable DocumentoCabByCod(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoCabByCod";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable DocumentoCabByCodForPago(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoCabByCodForPago";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
            
        }

        public DataTable DocumentoDetByCod(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoDetByCod";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable consultaDocumento(int ctip_docvta, int ccod_condvta, DateTime dfec_ini,
                DateTime dfec_fin, int cnum_serie, long cnum_desde, long cnum_hasta, int ccod_ruta,
                String ccod_modulo, int ccod_trans, int ccod_cli, int flg_anulados, int ctip_bus)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsConsultarDocumentos";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@dfec_ini", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@dfec_fin", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_desde", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@cnum_hasta", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters.Add("@ccod_trans", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_cli", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_anulados", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ctip_bus", SqlDbType.Int);

            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@ccod_condvta"].Value = ccod_condvta;
            sqlcmd.Parameters["@dfec_ini"].Value = dfec_ini;
            sqlcmd.Parameters["@dfec_fin"].Value = dfec_fin;
            sqlcmd.Parameters["@cnum_serie"].Value = cnum_serie;
            sqlcmd.Parameters["@cnum_desde"].Value = cnum_desde;
            sqlcmd.Parameters["@cnum_hasta"].Value = cnum_hasta;
            sqlcmd.Parameters["@ccod_ruta"].Value = ccod_ruta;
            sqlcmd.Parameters["@ccod_modulo"].Value = ccod_modulo;
            sqlcmd.Parameters["@ccod_trans"].Value = ccod_trans;
            sqlcmd.Parameters["@ccod_cli"].Value = ccod_cli;
            sqlcmd.Parameters["@flg_anulados"].Value = flg_anulados; 
            sqlcmd.Parameters["@ctip_bus"].Value = ctip_bus;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0]; 
        }


        public long existeDocumento(int ctip_docvta, int cnum_serie, long cnum_docvta)
        {
            long resul = 0;

            SqlCommand sqlcmd = new SqlCommand();

            sqlcmd.CommandText = "spsExisteDocumentoCab";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_cia", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_docvta", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_cia"].Value = Session.getParametros().ccod_cia;
            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@cnum_serie"].Value = cnum_serie;
            sqlcmd.Parameters["@cnum_docvta"].Value = cnum_docvta;

            resul = Convert.ToInt64(sqlcmd.ExecuteScalar()); 

            return resul; 
        }

        public resulMsgError autorizarDocumento(long ccod_doc, int ccod_user, int flg_autoriza)
        {
            resulMsgError resul = new resulMsgError(false, "");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spuAutorizaDocumento";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_user", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_autoriza", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
            sqlcmd.Parameters["@ccod_user"].Value = ccod_user;
            sqlcmd.Parameters["@flg_autoriza"].Value = flg_autoriza;

            try
            {
                sqlcmd.ExecuteNonQuery();
                resul.resul = true;
            }
            catch (Exception ex)
            {
                resul.message = ex.Message; 
            }
            return resul;
        }

        public resulMsgError documentosFromPedido(int ccod_cia, int cnum_serie, long cnum_inidoc, long cnum_findoc,
                    DateTime dfec_emi, DateTime dfec_ent, int ctip_docvta, int ccod_ruta, int ccod_cli)
        {
            resulMsgError resul = new resulMsgError(false, "");
            SqlTransaction transac = Session.getConexion().BeginTransaction("pedsdocs");
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spiDocumentosFromPedidos";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;

            sqlcmd.Parameters.Add("@ccod_cia", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_inidoc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@cnum_findoc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@dfec_emi", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_cli", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_cia"].Value = ccod_cia;
            sqlcmd.Parameters["@cnum_serie"].Value = cnum_serie;
            sqlcmd.Parameters["@cnum_inidoc"].Value = cnum_inidoc;
            sqlcmd.Parameters["@cnum_findoc"].Value = cnum_findoc;
            sqlcmd.Parameters["@dfec_emi"].Value = dfec_emi;
            sqlcmd.Parameters["@dfec_ent"].Value = dfec_ent;
            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@ccod_ruta"].Value = ccod_ruta;
            sqlcmd.Parameters["@ccod_cli"].Value = ccod_cli;
            sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
            sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;

            try
            {
                long nrodocs = Convert.ToInt64(sqlcmd.ExecuteScalar());
                if (nrodocs > 0)
                {
                    resul.resul = false;
                    resul.message = "Existen " + nrodocs.ToString() + " documento(s) ya generado(s) dentro del rango indicado.";
                }
                else
                {
                    transac.Commit();
                    resul.resul = true;
                    resul.message = "Se generaron los documentos satisfactoriamente.";
                }
            }
            catch (Exception ex)
            {
                transac.Rollback();
                resul.message = ex.Message;
            }
            return resul;
        }

        public DataTable MostrarDocumentosToAsignar(DateTime dfec_ent, int ctip_docvta, int ccod_ruta, String ccod_modulo, int ccod_cli)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsMostrarDocumentosToAsignar";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar, 15);
            sqlcmd.Parameters.Add("@ccod_cli", SqlDbType.Int);

            sqlcmd.Parameters["@dfec_ent"].Value = dfec_ent;
            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@ccod_ruta"].Value = ccod_ruta;
            sqlcmd.Parameters["@ccod_modulo"].Value = ccod_modulo;
            sqlcmd.Parameters["@ccod_cli"].Value = ccod_cli;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable MostrarDocumentosToAnular(DateTime dfec_ent, int ccod_trans, int ctip_docvta)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsMostrarDocumentosToAnular";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@ccod_trans", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);

            sqlcmd.Parameters["@dfec_ent"].Value = dfec_ent;
            sqlcmd.Parameters["@ccod_trans"].Value = ccod_trans;
            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0]; 

        }

        public DataTable MostrarDocumentosToLiquidar(DateTime dfec_ent, int ctip_docvta, int ccod_condvta, int ccod_trans, int ccod_ruta, String ccod_modulo)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsMostrarDocumentosToLiquidar";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_trans", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_ruta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_modulo", SqlDbType.NVarChar, 15);


            sqlcmd.Parameters["@dfec_ent"].Value = dfec_ent;
            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@ccod_condvta"].Value = ccod_condvta;
            sqlcmd.Parameters["@ccod_trans"].Value = ccod_trans;
            sqlcmd.Parameters["@ccod_ruta"].Value = ccod_ruta;
            sqlcmd.Parameters["@ccod_modulo"].Value = ccod_modulo;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0]; 
        }

        public resulMsgError activarDocumento(long ccod_doc, int ctip_docvta)
        {
            resulMsgError resul = new resulMsgError(false, "");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spuActivarDocumento";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();  

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

            try
            {
                sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
                sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;

                sqlcmd.ExecuteNonQuery();
 
                resul.message = "Se activo el documento satisfactoriamente";
                resul.resul = true;
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }

            return resul;
        }

        public resulMsgError anularDocumento(List<InfoDocumentoAnular> docsanuls)
        {
            resulMsgError resul = new resulMsgError(false, "");

            SqlCommand sqlcmd = new SqlCommand();

            //crear el objeto transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("anuldoc");
  
            sqlcmd.CommandText = "spuAnularDocumento";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;
 
            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_motanul", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

            try
            {
                //valores que no cambian en cada registro
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;
                

                foreach (InfoDocumentoAnular docanul in docsanuls)
                {
                    sqlcmd.Parameters["@ccod_doc"].Value = docanul.ccod_doc;
                    sqlcmd.Parameters["@ctip_docvta"].Value = docanul.ctip_docvta;
                    sqlcmd.Parameters["@ccod_motanul"].Value = docanul.ccod_motanul;
                    sqlcmd.ExecuteNonQuery(); 
                }
                transac.Commit(); 
                resul.resul = true;
                resul.message = "Se anularon los documentos satisfactoriamente.";
            }
            catch (Exception ex)
            {
                transac.Rollback(); 
                resul.message = ex.Message;
            }
            return resul;
        }

        public resulMsgError asignarTransportista(List<InfoDocumentoAsignar> docasigs, Boolean forzarasig)
        {
            resulMsgError resul = new resulMsgError(false, "");
 
            SqlCommand sqlcmd = new SqlCommand();
            //crear el objeto transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("asigtrans");
  
            sqlcmd.CommandText = "spuAsignarTransportista";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;
            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_trans", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar,50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

            try
            {
                //asignamos los valores inamovibles
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;
                
                //iniciar la transaccion pendiente.

                foreach (InfoDocumentoAsignar doca in docasigs)
                {
                    if (forzarasig)
                    {
                        sqlcmd.Parameters["@ccod_doc"].Value = doca.ccod_doc;
                        sqlcmd.Parameters["@ccod_trans"].Value = doca.ccod_trans;
                        sqlcmd.ExecuteNonQuery(); 
                    }
                    else 
                    {
                        if (doca.ccod_transasig == 0)
                        {
                            sqlcmd.Parameters["@ccod_doc"].Value = doca.ccod_doc;
                            sqlcmd.Parameters["@ccod_trans"].Value = doca.ccod_trans;
                            sqlcmd.ExecuteNonQuery(); 
                        }
                    }
                }
                transac.Commit();
                resul.resul = true;
                resul.message = "Se asignaron todos los documentos satisfactoriamente";
            }
            catch (Exception ex)
            {
                transac.Rollback(); 
                resul.message = ex.Message; 
            }

            return resul;
        }

        public DataTable DocumentoPagoByCodDoc(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoPagoByCodDoc";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0]; 
        }

        public resulMsgError liquidarDocumento(List<InfoDocumentoLiquidar> docsliqs)
        {
            resulMsgError resul = new resulMsgError(false, "");
 
            SqlCommand sqlcmd = new SqlCommand();
            //crear el objeto transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction();  

            sqlcmd.CommandText = "spuLiquidarDocumento";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;

            sqlcmd.Parameters.Add("@dfec_pago", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_cobra", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_tipopago", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ccod_docnc", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@nimp_pago", SqlDbType.Decimal);
            sqlcmd.Parameters.Add("@nimp_pagoliq", SqlDbType.Decimal);
            sqlcmd.Parameters.Add("@ntot_cre", SqlDbType.Decimal);
            sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar,50);
            sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);


            try
            {
                //establecer valores inamovibles
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;
   
                foreach (InfoDocumentoLiquidar docliq in docsliqs)
                {
                    sqlcmd.Parameters["@dfec_pago"].Value = docliq.dfec_pago;
                    sqlcmd.Parameters["@ccod_doc"].Value = docliq.ccod_doc;
                    sqlcmd.Parameters["@ccod_cobra"].Value = docliq.ccod_cobra; 
                    sqlcmd.Parameters["@ccod_tipopago"].Value = docliq.ccod_tipopago;
                    //verificamos si es ccod_docpago=0 entonces ponemos null
                    if (docliq.ccod_docnc == 0)
                    {
                        sqlcmd.Parameters["@ccod_docnc"].Value = DBNull.Value;
                    }
                    else
                    {
                        sqlcmd.Parameters["@ccod_docnc"].Value = docliq.ccod_docnc;
                    }
                    sqlcmd.Parameters["@nimp_pago"].Value = docliq.nimp_pago;
                    sqlcmd.Parameters["@nimp_pagoliq"].Value = docliq.nimp_pagoliq;
                    sqlcmd.Parameters["@ntot_cre"].Value = docliq.ntot_cre;
                    sqlcmd.ExecuteNonQuery();
                }
                transac.Commit();
                resul.resul = true;
                resul.message = "Se liquidaron los documentos satisfactoriamente.";
            }
            catch (Exception ex)
            {
                transac.Rollback(); 
                resul.message = ex.Message; 
            }
            return resul;
        }




    }
}
